package com.sskj.gcc.market;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.rxbus2.BusData;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.market.adapter.MarketDetailAdapter;
import com.sskj.gcc.market.data.MarketDetail;
import com.sskj.level.bean.CoinType;
import com.sskj.level.ui.activity.LevelActivity;
import com.sskj.lib.Constants;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.CoinBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * K线的fragment
 */
@Route(path = RConfig.APP_MAKETETDETAILFRAGMENT)
public class MarketDetailFragment extends BaseFragment<MarketDetailFragmentPresenter> {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    MarketDetailAdapter detailAdapter;
    @BindView(R.id.ivTopLeft)
    ImageView leftImg;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.btTrade)
    Button btTrade;

    String coinName = "USDT";

    @Override
    protected int getLayoutId() {
        return R.layout.market_detail_fragment;
    }

    @Override
    protected MarketDetailFragmentPresenter getPresenter() {
        return new MarketDetailFragmentPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        RxBus.getDefault().register(this);
        //防止请求数据过慢，设置btc为标题
        tvTitle.setText(coinName.replace("_", "/").toUpperCase());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        detailAdapter = new MarketDetailAdapter(null, getActivity());
        recyclerView.setAdapter(detailAdapter);
        //跳转levelActivity
        ClickUtil.click(btTrade, () -> {
            RxBus.getDefault().post(new CoinType(coinName));
            RxBus.getDefault().send(RxBusCode.MINING_LEVEL);
            LevelActivity.start(getActivity(),coinName);
        });
        ClickUtil.click(leftImg, () -> {
            ARouter.getInstance().build(RConfig.KLEVEL_SLIDE).withBoolean(Constants.ISKLEVEL,true).navigation();
        });
    }

    @Override
    public void initData() {
        mPresenter.getProduct("");
    }

    /**
     * 请求币种第一条数据的code做原数据源
     * @param data
     */
    public void setData(List<CoinBean> data) {
        if (data != null && data.size() > 0) {
            CoinBean coinBean = data.get(0);
            updateData(coinBean);
            coinName = coinBean.getCode();
            tvTitle.setText(coinBean.getCode().replace("_", "/").toUpperCase());
        }
    }

    /**
     * K线赋值
     * @param coinBean
     */
    public void updateData(CoinBean coinBean) {
        coinName = coinBean.getCode();
        tvTitle.setText(coinBean.getCode().toUpperCase().replace("_", "/"));
        List<MarketDetail> detailData = new ArrayList<>();
        MarketDetail topData = new MarketDetail(MarketDetail.TOP);
        topData.setTopData(coinBean);
        detailData.add(topData);
        detailData.add(new MarketDetail(MarketDetail.CHART));
        detailData.add(new MarketDetail(MarketDetail.BOTTOM));
        detailAdapter.setNewData(detailData);
    }


    /**
     * 更新价格
     * MainActivity.WebSocket
     *
     * @param data
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updatePrice(CoinBean data) {
        if (data.getCode().equals(coinName)) {
            updateData(data);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        detailAdapter = null;
        RxBus.getDefault().unregister(this);
    }

    /**
     * K线点击抽屉切换code
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeCoin(BusData<CoinBean> busData) {
        if ("KLevel".equals(busData.getId())) {
            coinName = busData.getData().getCode();
            tvTitle.setText(coinName.replace("_", "/").toUpperCase());
            updateData(busData.getData());
        }
    }

}
